const baseUrl = "http://localhost:3000";
// const baseUrl="https://8cac1d82.ngrok.io";
const formatTime = date => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  return (
    [year, month, day].map(formatNumber).join("/") +
    " " +
    [hour, minute, second].map(formatNumber).join(":")
  );
};

const formatNumber = n => {
  n = n.toString();
  return n[1] ? n : "0" + n;
};
/*
     wx rquest
      {
        url: url, //不为空
        successFun: successFun,//不为空
        failFun: failFun,
        completeFun: completeFun,
        method: method,
        data: data,
        query:query
      }
*/
const http = obj => {
  const requestTask = wx.request({
    url: obj.url + addQuery(obj.query),
    header: {
      "content-type": "application/json"
    },
    data: obj.data == undefined ? "" : obj.data,
    method: obj.method == undefined ? "GET" : obj.method,
    dataType: "json",
    success(data) {
      obj.success(data.data);
    },
    fail(err) {
      obj.fail == undefined ? null : obj.fail(err);
    },
    complete(res) {
      obj.complete == undefined ? null : obj.complete(res);
    }
  });
  return requestTask;
};
const formatPlayCount = count => {
  return count / 10000 >= 1 ? count / 10000 + "万" : count;
};
const customLoading = () => {
  wx.showLoading({
    title: "加载中...",
    mask: true
  });
};

const bgMusic = {
  bgPlay(url, title, coverImgUrl, success, fail, complete) {
    const backgroundAudioManager = wx.getBackgroundAudioManager();
    wx.playBackgroundAudio({
      dataUrl: url,
      title: title,
      coverImgUrl: coverImgUrl,
      success: function (res) {
        success == undefined ? null : success(res);
      },
      fail: function (error) {
        fail == undefined ? null : fail(error);
      },
      complete: function () {
        complete == undefined ? null : complete();
      }
    });
  },
  bgPause() {
    wx.pauseBackgroundAudio();
  },
  bgSeek(position, success, fail, complete) {
    wx.seekBackgroundAudio({
      position: position,
      success(data) {
        success == undefined ? null : success(data);
      },
      fail(error) {
        fail == undefined ? null : fail(error);
      },
      complete(res) {
        complete == undefined ? null : complete(res);
      }
    });
  },
  bgStop() {
    wx.stopBackgroundAudio();
  },
  onBgPlay(fn) {
    wx.onBackgroundAudioPlay(data => {
      fn == undefined ? null : fn(data);
    });
  },
  onBgPause(fn) {
    wx.onBackgroundAudioPause(data => {
      fn == undefined ? null : fn(data);
    });
  },
  onBgStop(fn) {
    wx.onBackgroundAudioStop(data => {
      fn == undefined ? null : fn(data);
    });
  }
};
const customTrim = n => {
  return n.replace(/^\s+|\s+$/gm, '')
}
const coustomToast = (obj) => {
  wx.showToast({
    title: obj.title,
    icon: 'none'
  })
}
module.exports = {
  baseUrl: baseUrl,
  formatTime: formatTime,
  http: http,
  customLoading: customLoading,
  bgMusic: bgMusic,
  customTrim: customTrim,
  coustomToast:coustomToast
};

function addQuery(obj) {
  if (!obj) {
    return "";
  }
  let str = "";
  for (let i in obj) {
    str += i + "=" + obj[i] + "&";
  }
  return "?" + str.substring(0, str.length - 1);
}
